package com.qaml.mes.device.domain.service;

import com.qaml.mes.device.api.CheckUpkeepItemService;
import com.qaml.mes.device.dao.CheckUpkeepItemDO;
import com.qaml.mes.device.enums.DeletedEnum;
import com.qaml.mes.device.repository.CheckUpkeepItemRepository;
import com.qaml.mes.device.domain.convert.CheckUpkeepItemConvert;
import com.qaml.mes.device.vo.eqp.CheckUpkeepItemVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 检保信息条目表 服务实现类
 * </p>
 *
 * @author leikai
 * @since 2024-02-27
 */
@Service
public class CheckUpkeepItemServiceImpl implements CheckUpkeepItemService {

    @Resource
    private CheckUpkeepItemRepository checkUpkeepItemRepository;

    /**
     * 根据检保信息id获取检保信息条目
     *
     * @param checkUpkeepId 检保信息id
     * @return 检保信息条目
     */
    @Override
    public List<CheckUpkeepItemVO> getCheckUpkeepItemVOList(String checkUpkeepId) {
        List<CheckUpkeepItemDO> checkUpkeepItemDOS = checkUpkeepItemRepository.getByCheckUpkeepId(checkUpkeepId);
        return CheckUpkeepItemConvert.INSTANCE.toVOlist(checkUpkeepItemDOS);
    }

    /**
     * 根据检保信息id删除检保信息条目
     *
     * @param checkUpkeepId 检保信息id
     */
    @Override
    public void deleteByCheckUpkeepId(String checkUpkeepId) {

        // 1,获取检保信息条目
        List<CheckUpkeepItemDO> checkUpkeepItemDOS = checkUpkeepItemRepository.getByCheckUpkeepId(checkUpkeepId);

        // 获取对应的检保信息条目 Ids
        List<String> ids = checkUpkeepItemDOS.stream().map(CheckUpkeepItemDO::getId).collect(Collectors.toList());
        // 批量删除
        // 批量修改，设置isDeleted
        checkUpkeepItemRepository.updateCheckUpItemByIds(ids);
    }

    @Override
    public void deleteDataByCheckUpkeepId(String checkUpkeepId) {
        CheckUpkeepItemDO checkUpkeepItemDO = new CheckUpkeepItemDO();
        checkUpkeepItemDO.setCheckUpkeepId(checkUpkeepId);
        checkUpkeepItemDO.setIsDeleted(DeletedEnum.YES.getCode());
        checkUpkeepItemRepository.updateByCheckUpKeepId(checkUpkeepItemDO);
    }


}
